if exists('g:AutoPairsLoaded') || &cp
  finish
endif
let g:AutoPairsLoaded = 1

if !exists('g:AutoPairs')
  let g:AutoPairs = {'(':')', '{':'}', '[':']', "'":"'", '"':'"', '`':'`', "'''":"'''", '"""':'"""', '```':'```'}
endif

" default pairs base on filetype
function AutoPairsDefaultPairs() 
  if exists('b:autopairs_defaultpairs')
    return b:autopairs_defaultpairs
  endif
  let r = copy(g:AutoPairs)
  let allPairs = {
  \ 'vim': {'\v^\s*\zs"': ''},
  \ 'rust': {'\w\zs<': '>', '&\zs''': ''},
  \ }
  for [filetype, pairs] in items(allPairs)
    if &filetype == filetype
      for [open, close] in items(pairs)
        let r[open] = close
      endfor
    endif
  endfor
  let b:autopairs_defaultpairs = r
  return r
endfunction

if !exists('g:AutoPairsMapBS')
  let g:AutoPairsMapBS = 1
endif

" Map <C-h> as the same BS
if !exists('g:AutoPairsMapCh')
  let g:AutoPairsMapCh = 1
endif

if !exists('g:AutoPairsMapCR')
  let g:AutoPairsMapCR = 1
endif

if !exists('g:AutoPairsWildClosedPair')
  let g:AutoPairsWildClosedPair = 1
endif

if !exists('g:AutoPairsMapSpace')
  let g:AutoPairsMapSpace = 1
endif

if !exists('g:AutoPairsCenterLine')
  let g:AutoPairsCenterLine = 1
endif

if !exists('g:AutoPairsShortcutToggle')
  let g:AutoPairsShortcutToggle = '<M-p>'
endif

if !exists('g:AutoPairsShortcutFastWrap')
  let g:AutoPairsShortcutFastWrap = '<M-e>'
endif

if !exists('g:AutoPairsMoveCharacter')
  let g:AutoPairsMoveCharacter = "()[]{}\"'"
endif

if !exists('g:AutoPairsShortcutJump')
  let g:AutoPairsShortcutJump = '<M-n>'
endif

if !exists("g:AutoPairsFlyMode")
  let g:AutoPairsFlyMode = 0
endif

if !exists('g:AutoPairsMultilineClose')
  let g:AutoPairsMultilineClose = 1
endif

if !exists('g:AutoPairsShortcutBackInsert')
  let g:AutoPairsShortcutBackInsert = '<M-b>'
endif

if !exists('g:AutoPairsSmartQuotes')
  let g:AutoPairsSmartQuotes = 1
endif

if v:version > 704 || v:version == 704 && has("patch849")
  let s:Go = "\<C-G>U"
else
  let s:Go = ""
endif

let s:Left = s:Go."\<LEFT>"
let s:Right = s:Go."\<RIGHT>"


" unicode len
func! s:ulen(s)
  return len(split(a:s, '\zs'))
endf

func! s:left(s)
  return repeat(s:Left, s:ulen(a:s))
endf

func! s:right(s)
  return repeat(s:Right, s:ulen(a:s))
endf

func! s:delete(s)
  return repeat("\<DEL>", s:ulen(a:s))
endf

func! s:backspace(s)
  return repeat("\<BS>", s:ulen(a:s))
endf

func! s:getline()
  let line = getline('.')
  let pos = col('.') - 1
endf
